package okhttp.itydl.com.download.file;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

import okhttp.itydl.com.download.utils.Md5Utils;

/**
 * 文件管理类
 *
 * @author howard
 */
public class FileStorageManager {

    private static final FileStorageManager storageManager = new FileStorageManager();
    private Context mContext;

    private FileStorageManager() {

    }

    public static FileStorageManager getInstance() {
        return storageManager;
    }

    public void init(Context context) {
        this.mContext = context;
    }

    public File getFileByName(String url) {
        File parent;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            parent = mContext.getExternalCacheDir();
        } else {
            parent = mContext.getCacheDir();
        }

        File file = new File(parent, Md5Utils.generateCode(url));

        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file;
    }

}
